package com.zmn.plat.admin.controller.appointment.appointmentsetting;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import com.zmn.plat.model.entity.appointment.appointmentsetting.AppointmentSettingQuery;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.number.NumberUtil;
import com.zmn.plat.model.entity.appointment.appointmentsetting.AppointmentSetting;
import com.zmn.plat.services.interfaces.appointment.appointmentsetting.AppointmentSettingService;

/**
 * 预约设置基础修改Controller
 *
 * @author xujie
 * @since 2020-09-04 16:29:25
 */
@Slf4j
@RestController
@RequestMapping("/appointment/appointmentsetting")
public class AppointmentSettingModifyController extends AppointmentSettingBaseController {

    @Resource
    private AppointmentSettingService appointmentSettingService;

    /**
     * 批量保存
     */
    @RequestMapping("/addEditList")
    @RequiresPermissions("/appointment/appointmentsetting/addEditList.action")
    public ResponseDTO addEditList(HttpServletRequest request, @RequestBody List<AppointmentSetting> settings) {

        Date now = DateUtil.getNow();
        List<AppointmentSetting> insetBatchList = new ArrayList<>();
        List<AppointmentSetting> updateBatchList = new ArrayList<>();
        for (AppointmentSetting setting : settings) {
            int id = setting.getId();
            if (id == 0) {
                AppointmentSettingQuery query = AppointmentSettingQuery.builder()
                        .appointmentOrChange(setting.getAppointmentOrChange())
                        .plat(setting.getPlat())
                        .type(setting.getType())
                        .build();
                List<AppointmentSetting> appointmentSettings = appointmentSettingService.listByQuery(query);
                if(appointmentSettings == null || appointmentSettings.size() == 0){
                    setting.setCreater(getMcStaffRealName());
                    setting.setCreateTime(now);
                    setting.setUpdateTime(now);
                    insetBatchList.add(setting);
                }else if(appointmentSettings.size() == 1){
                    AppointmentSetting appointmentSetting = appointmentSettings.get(0);
                    setting.setId(appointmentSetting.getId());
                }
            }
            if(id != 0){
                setting.setUpdater(getMcStaffRealName());
                setting.setUpdateTime(now);
                updateBatchList.add(setting);
            }
        }
        if (insetBatchList.size() > 0) {
            appointmentSettingService.insertBatch(insetBatchList);
        }
        if (updateBatchList.size() > 0) {
            appointmentSettingService.updateBatchByKey(updateBatchList);
        }
        return ResponseDTO.success(true, "操作成功！");

    }

}
